/**
 * Класс таблиц базы данных для панели администрирования
 * @lends phonebook.view.AdminView
 */
phonebook.view.AdminView = Base.extend({
	
    controller : null,		// объект управления процессами Приложения
    contextMenu: null,
    orgUnitTypeTable: null,
    orgUnitTable: null,
    postTable: null,
    phoneTypeTable: null,
    phoneTable: null,
    roomTable: null,
    personTable: null,
    orgUnitPostTable: null,
    /**
     * Конструктор класса
     * @author Konstantin Chumachenko
     * 
     * @augments Base
     * @class Класс таблиц базы данных для панели администрирования
     * 
     * <p><b>Пример:</b></p>
     * <pre class="code">
     * var AdminView = new phonebook.view.AdminView(controller);
     * </pre>
     * 
     * @constructs
     * @param {Controller} controller Контроллер приложения.
     */
    constructor : function(controller) {	
        console.log("-- AdminView: Конструктор.");
        this.controller = controller;
        this.domElement = $(MAIN_HOLDER);
        this.render(this.domElement);
    },
	
    /**
     * Возвращает DOM-элемент этой панели 
     * @function
     * @public 
     * @memberOf phonebook.view.AdminView#
     */
    getDomElement : function() {
        return this.domElement;
    },
	
    /**
     * Выполняется отрисовка.
     * @private 
     * @function
     * @param (elementDOM) renderTo Элемент DOM, внутри которого будет создан контейнер.
     * @return none
     */
    render : function(renderTo) {
        console.log("-- AdminView2: render.");
        var me = this;      
        var model = me.controller.bigModel.getModel();
        var turnText = "[TURN]";
        var orgUnitType = 1,
        orgUnit = 1,
        post = 1,
        phoneType = 1,
        phone = 1,
        room = 1,
        person = 1,
        orgUnitPost = 1;
        me.contextMenu = new widget.PopupMenu({
            width : 200,
            items : [             
            {
              value:2,
              text:"Добавить"
            },
            {                
                value:1, 
                text:'Удалить'
            }
            ],
            listeners : {
                select : function(menu, menuItem, gridRecord){
                    if (menuItem.value==1){
                        me.controller.deleteById(gridRecord[0],gridRecord.pop());
                    }
                     if (menuItem.value==2){
                        me.controller.showAddDialog(gridRecord.pop());
                    }
                }
            }
        });
        /**
         * Таблица OrgUnitType
         */
        $(ORG_UNIT_TYPE_TABLE_HOLDER).append( '<h3 id="tblHeader">OrgUnitType</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: ORG_UNIT_TYPE_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (orgUnitType==1){
                        orgUnitType = 0;                    
                        $("#orgUnitType_wrapper").hide();    
                    }else{
                        orgUnitType = 1;                    
                        $("#orgUnitType_wrapper").show();    
                    }
                    
                }
            }
        });
        
        $(ORG_UNIT_TYPE_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="orgUnitType"></table>' );
        me.orgUnitTypeTable = $('#orgUnitType').dataTable( {
            "bFilter": false,                
            "aaData":model["orgUnitType"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"OrgUnitType");
                    return false;
                });
                
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "name"
            }
            ]
        } );   
        
        /**
         * Таблица OrgUnit
         */
        $(ORG_UNIT_TABLE_HOLDER).append( '<h3 id="tblHeader">OrgUnit</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: ORG_UNIT_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (orgUnit==1){
                        orgUnit = 0;                    
                        $("#orgUnit_wrapper").hide();    
                    }else{
                        orgUnit = 1;                    
                        $("#orgUnit_wrapper").show();    
                    }
                    
                }
            }
        });
        $(ORG_UNIT_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="orgUnit"></table>' );
        me.orgUnitTable = $('#orgUnit').dataTable( {
            "bFilter": false,     
            "aaData":model["orgUnit"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"OrgUnit");
                    return false;
                });
                
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "name"
            },
            {
                "sTitle": "unitType ID"
            },
            {
                "sTitle": "perent unit id"
            }
            ]
        } );   
        /**
         * Таблица Post
         */          
        $(POST_TABLE_HOLDER).append( '<h3 id="tblHeader">Post</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: POST_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (post==1){
                        post = 0;                    
                        $("#post_wrapper").hide();    
                    }else{
                        post = 1;                    
                        $("#post_wrapper").show();    
                    }
                    
                }
            }
        });
        $(POST_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="post"></table>' );
        me.postTable = $('#post').dataTable( {
            "bFilter": false,     
            "aaData":model["post"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"Post");
                    return false;
                });
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "name"
            }
            ]
        } );   
        /**
         * Таблица PhoneType
         */          
        $(PHONE_TYPE_TABLE_HOLDER).append( '<h3 id="tblHeader">PhoneType</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: PHONE_TYPE_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (phoneType==1){
                        phoneType = 0;                    
                        $("#phoneType_wrapper").hide();    
                    }else{
                        phoneType = 1;                    
                        $("#phoneType_wrapper").show();    
                    }
                    
                }
            }
        });
        $(PHONE_TYPE_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="phoneType"></table>' );
        me.phoneTypeTable = $('#phoneType').dataTable( {
            "bFilter": false,     
            "aaData":model["phoneType"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"PhoneType");
                    return false;
                });
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "name"
            }
            ]
        } );   
        /**
         * Таблица Phone
         */
        $(PHONE_TABLE_HOLDER).append( '<h3 id="tblHeader">Phone</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: PHONE_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (phone==1){
                        phone = 0;                    
                        $("#phone_wrapper").hide();    
                    }else{
                        phone = 1;                    
                        $("#phone_wrapper").show();    
                    }
                    
                }
            }
        });
        $(PHONE_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="phone"></table>' );
        me.phoneTable = $('#phone').dataTable( {
            "bFilter": false,     
            "aaData":model["phone"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"Phone");
                    return false;
                });
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "number"
            },
            {
                "sTitle": "UnitPost Id"
            },
            {
                "sTitle": "PhoneType Id"
            }
            ]
        } );   
        /**
         * Таблица Room
         */
        $(ROOM_TABLE_HOLDER).append('<h3 id="tblHeader">Room</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: ROOM_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (room==1){
                        room = 0;                    
                        $("#room_wrapper").hide();    
                    }else{
                        room = 1;                    
                        $("#room_wrapper").show();    
                    }
                    
                }
            }
        });
        $(ROOM_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="room"></table>' );
        me.roomTable = $('#room').dataTable( {
            "bFilter": false,     
            "aaData":model["room"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"Room");
                    return false;
                });
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "name"
            }
            ]
        } );   
        /**
         * Таблица Person        
         */
        $(PERSON_TABLE_HOLDER).append( '<h3 id="tblHeader">Person</h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: PERSON_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (person==1){
                        person = 0;                    
                        $("#person_wrapper").hide();    
                    }else{
                        person = 1;                    
                        $("#person_wrapper").show();    
                    }
                    
                }
            }
        });
        $(PERSON_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="person"></table>' );
        me.personTable = $('#person').dataTable( {
            "bFilter": false,     
            "aaData":model["person"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"Person");
                    return false;
                });
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "surname"
            },
            {
                "sTitle": "name"
            },
            {
                "sTitle": "patronymic"
            }
            
            ]
        } );   
        
        /**
         * Таблица orgUnitPost      
         * id, unit id, person id, post id, room id.   
         */
        $(ORG_UNIT_POST_TABLE_HOLDER).append( '<h3 id="tblHeader">OrgUnitPost </h3>');
        new widget.ActiveLink({
            text : turnText,
            renderTo: ORG_UNIT_POST_TABLE_HOLDER,
            listeners : {
                click : function(lnk, elementDom){
                    if (orgUnitPost==1){
                        orgUnitPost = 0;                    
                        $("#orgUnitPost_wrapper").hide();    
                    }else{
                        orgUnitPost = 1;                    
                        $("#orgUnitPost_wrapper").show();    
                    }
                    
                }
            }
        });
        $(ORG_UNIT_POST_TABLE_HOLDER).append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="orgUnitPost"></table>' );        
        me.orgUnitPostTable = $('#orgUnitPost').dataTable( {
            "bFilter": false,     
            "aaData":model["orgUnitPost"],
            "fnRowCallback":function( nRow, aData, iDisplayIndex ) {
                $(nRow).bind('contextmenu', function(event) {
                    me.onContextMenu(aData, nRow, event,"OrgUnitPost");
                    return false;
                });
            }, 
            "aoColumns": [
            {
                "sTitle": "id"
            },
            {
                "sTitle": "unit id"
            },
            {
                "sTitle": "person id"
            },
            {
                "sTitle": "room id"
            },
            {
                "sTitle": "post id"
            }
            ]
        } );
    },
    onContextMenu : function(record, element, event, table) {        
        record.push(table);
        var menu = this.contextMenu;	
        var position = {
            x : event.pageX, 
            y : event.pageY
        };
        menu.render(position, record);
    }
	
});